package com.ccp.dev.system.tag;

import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.util.AppUtil;
import com.ccp.dev.system.model.Dictionary;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.system.service.DictionaryService;
import com.ccp.dev.system.service.SysUserService;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.util.List;

public class SecSelectTag extends SimpleTagSupport {

	/**
	 * 数据来源，信息密级，默认
	 */
	private static final short DATA_SRC_INFOSEC=1;
	/**
	 * 数据来源，人员密级
	 */
	private static final short DATA_SRC_USERSEC=2;
	
	
	/**
	 * 数据字典中信息密级节点名称，对应SYS_GL_TYPE表
	 */
	private static final String NODEKEY_INFO = "sjmj";
	/**
	 * 数据字典中人员密级节点名称，对应SYS_GL_TYPE表
	 */
	private static final String NODEKEY_USER = "rymj";
	
	
	/**
	 * 数据来源，默认取信息密级
	 */
	private short data_src = DATA_SRC_INFOSEC;

	/**
	 * 当前实体的密级，比如当前用户的密级，当前消息的密级等
	 */
	private int nowSec;

	/**
	 * 下拉列表控件的名称
	 */
	private String id;

	/**
	 * 下拉列表控件的名称
	 */
	private String name;

	/**
	 * 下拉列表控件的名称
	 */
	private String css;

	/**
	 * 下拉列表的宽度
	 */
	private int width;
	
	public short getData_src() {
		return data_src;
	}
	public void setData_src(short data_src) {
		this.data_src = data_src;
	}

	public int getNowSec() {
		return nowSec;
	}
	public void setNowSec(int nowSec) {
		this.nowSec = nowSec;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getCss() {
		return css;
	}

	public void setCss(String css) {
		this.css = css;
	}

	@Override
	public void doTag() throws JspException {
		JspWriter out=getJspContext().getOut();
		try{
			DictionaryService dictionaryService = (DictionaryService) AppUtil.getBean(DictionaryService.class);
			SysUserService sysUserService = (SysUserService) AppUtil.getBean(SysUserService.class);
			String nodeKey="";
			int sec=0;
			//数据来源为信息密级
			if(data_src==DATA_SRC_INFOSEC){
				nodeKey = NODEKEY_INFO;
			//数据来源为人员密级
			}else if(data_src==DATA_SRC_USERSEC){
				nodeKey = NODEKEY_USER;
			}
			List<Dictionary> list=dictionaryService.getByNodeKey(nodeKey);
			//取用户密级，根据用户密级过滤
			String userId = ContextUtil.getCurrentUserId();
			SysUser sysUser = sysUserService.getById(userId);
			sec = sysUser.getPrivacyLevel();
			StringBuffer content = new StringBuffer("<select ");
			if(id != null) {
				content.append("id=\"" + id + "\" ");
			}
			if(name != null) {
				content.append("name=\"" + name + "\" ");
			}
			if(css != null) {
				content.append("class=\"" + css + "\" ");
			}
			if(width != 0){
				content.append("style=\"width:" + width + "px\" ");
			}
			content.append(">");
			for(Dictionary dic :list){
				if(new Integer(dic.getItemValue())>sec){
					continue;
				}
				content.append("<option value="+dic.getItemValue());
				if(nowSec==new Integer(dic.getItemValue())){
					content.append(" selected ");
				}
				content.append(">"+dic.getItemName()+"</option>");
			}
			content.append("</select>");
			out.print(content);
		}catch(Exception ex){
			 ex.printStackTrace();
		}
	}
}
